package com.nut.course.api.biz;

import com.nut.course.api.req.TagReq;
import com.nut.course.api.req.TagUsersReq;
import com.nut.course.common.Result;
import com.nut.course.service.TagService;
import com.nut.course.service.TagUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.bean.WxCpTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    @Autowired
    private TagUserService tagUserService;

    @RequestMapping(value = "/add")
    public Result add(@RequestParam String name) {
        return tagService.create(name);
    }

    @PostMapping(value = "/update")
    public Result update(@RequestBody TagReq tagReq) {
        return tagService.update(tagReq.getName(), tagReq.getId());
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public Result test(String json) {
        log.info(json);
        return Result.ok();
    }

    @RequestMapping(value = "/delete")
    public Result delete(@RequestParam String id) {
        return tagService.delete(id);
    }

    @RequestMapping(value = "/getById")
    public Result getById(@RequestParam String id) {
        return tagService.getById(id);
    }

    @PostMapping(value = "/addTagUsers")
    public Result addTagUsers(@RequestBody TagUsersReq tagUsersReq) {
        return tagService.addTagUsers(tagUsersReq);
    }

    @PostMapping(value = "/delTagUsers")
    public Result delTagUsers(@RequestBody TagUsersReq tagUsersReq) {
        return tagService.delTagUsers(tagUsersReq);
    }

    @RequestMapping(value = "/list")
    public Result<List<WxCpTag>> list() {
        return tagService.list();
    }

    @GetMapping(value = "/syncData")
    public Result syncData() {
        return tagUserService.syncTagData();
    }

}
